package com.course.design.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.EncryptUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.course.design.common.ResultCode;
import com.course.design.constants.Constant;
import com.course.design.dao.UserFriendMapper;
import com.course.design.dao.YgxxMapper;
import com.course.design.entity.Ygxx;
import com.course.design.entity.vo.FriendsVO;
import com.course.design.entity.vo.UserLoginVO;
import com.course.design.exception.ExecuteException;
import com.course.design.service.YgxxService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 描述:
 *
 * @author qinlianji
 * @create 2020-02-08
 */
@Service
public class YgxxServiceImpl implements YgxxService {

    @Autowired
    private YgxxMapper ygxxMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(Ygxx ygxx) {
        if (!StringUtils.isEmpty(ygxx.getDlmm())) {
            ygxx.setDlmm(EncryptUtils.md5Base64(ygxx.getDlmm()));
        }
        boolean result = false;
        if (StringUtils.hasLength(ygxx.getYgshId())) {
            result = ygxxMapper.updateById(ygxx) > 0;
        } else {
            // 新增用户如果密码为空 则默认密码
            if (StringUtils.isEmpty(ygxx.getDlmm())) {
                ygxx.setDlmm(EncryptUtils.md5Base64(Constant.DEFAULT_PASSWORD));
            }
            result = ygxxMapper.insert(ygxx) > 0;
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(String ygshId) {
        ygxxMapper.deleteById(ygshId);
    }

    @Override
    public void deleteReal(String ygshId) {
        ygxxMapper.deleteReal(ygshId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDeleteById(String ygshId) {
        ygxxMapper.batchDelete(ygshId);
    }

    @Override
    public Ygxx queryById(String ygshId) {
        return ygxxMapper.selectById(ygshId);
    }

    @Override
    public List<Ygxx> queryByDeptdmCd(String deptdmCd) {
        return ygxxMapper.queryByDeptdmCd(deptdmCd);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean changePassword(String password, String ygshId) {
        Ygxx ygxx = new Ygxx();
        ygxx.setYgshId(ygshId);
        ygxx.setDlmm(password);
        return ygxxMapper.updateById(ygxx) > 0;
    }

    @Override
    public Ygxx login(UserLoginVO vo) {
        vo.setPassword(EncryptUtils.md5Base64(vo.getPassword()));
        Ygxx login = ygxxMapper.login(vo);
        // 置空，防止泄露
        login.setDlmm(null);
        login.setCommand(null);
        return login;
    }

    @Override
    public List<FriendsVO> queryFriends(String userId) {
        List<FriendsVO> friends = ygxxMapper.queryFriends(userId);
        String szm = "";
        for (FriendsVO vo : friends){
            if (szm.equals(vo.getXmszm())){
                vo.setXmszm(null);
            }
            szm = vo.getXmszm();
        }
        return friends;
    }

    @Override
    public List<FriendsVO> queryDeptFriends(String userId) {
        return ygxxMapper.queryDeptFriends(userId);
    }

    @Override
    public IPage<Ygxx> queryPage(int start, int limit, String username) {
        return ygxxMapper.queryPage(new Page<>(start, limit), username);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Ygxx ygUpdateSelf(Ygxx ygxx) {
        LambdaQueryWrapper<Ygxx> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Ygxx::getYgshId, ygxx.getYgshId()).eq(Ygxx::getCommand, ygxx.getCommand());
        Ygxx selectOne = ygxxMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(selectOne)) {
            throw new ExecuteException(ResultCode.Error_Command);
        }
        ygxxMapper.updateById(ygxx);
        return ygxx;
    }

    @Override
    public IPage<Ygxx> querySqPage(int start, int limit, String username) {
        return ygxxMapper.querySqPage(new Page<>(start, limit), username);
    }

    @Override
    public void updateSjyxbj(String ygshId) {
        ygxxMapper.updateSjyxbj(ygshId);
    }
}
